﻿using System;
using System.Collections.Generic;
using System.Text;
using SIOsystem.Model.ClassifyDataSetTableAdapters;
using SIOsystem.Model.OrderDataSetTableAdapters;
using System.Data;
using System.Data.OleDb;

namespace SIOsystem.Control
{
    class ClassifyClass
    {
        static public bool insertClassify(long mainCID, string productName, string typeName
            , double defaultPrice, string unit)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    OleDbConnection conn = db.Connection;
                    conn.Open();
                    OleDbTransaction tr = conn.BeginTransaction();
                    db.Transaction = tr;
                    long autoID = db.InsertClassify(productName, mainCID, 2, null, null);
                    if (autoID > 0)
                    {
                        OleDbCommand cmd = conn.CreateCommand();
                        cmd.Transaction = tr;
                        cmd.CommandText = @"select @@identity";
                        autoID = long.Parse(cmd.ExecuteScalar().ToString());
                    }
                    else
                    {
                        tr.Rollback();
                        return false;
                    }
                    if (typeName.Equals(""))
                    {
                        typeName = "无规格";
                    }
                    autoID = db.InsertClassify(typeName, autoID, 3, defaultPrice, unit);
                    if (autoID > 0)
                    {
                        tr.Commit();
                        return true;
                    }
                    else
                    {
                        tr.Rollback();
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("添加材料出错", ex);
            }
        }

        static public bool insertClassify(string mainName)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {

                    long autoID = db.InsertClassify(mainName, 0, 1, null, null);
                    if (autoID > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("添加大类出错", ex);
            }
        }

        static public bool insertClassify(long productCID, string typeName, double defaultPrice, string unit)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {

                    long autoID = db.InsertClassify(typeName, productCID, 3, defaultPrice, unit);
                    if (autoID > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("添加规格出错", ex);
            }
        }

        static public bool updateClassify(long mainCID, string mainName)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    long autoID = db.UpdateClassify(mainName, null, null, mainCID);
                    if (autoID > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("修改大类出错", ex);
            }
        }

        static public bool updateClassify(long typeCID, string typeName, double defaultPrice, string unit)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    long autoID = db.UpdateClassify(typeName, defaultPrice, unit, typeCID);
                    if (autoID > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("修改规格出错", ex);
            }
        }

        static public bool updateClassify(long typeCID,decimal defaultPrice)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    long autoID = db.UpdateDefaultPrice(defaultPrice, typeCID);
                    if (autoID > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("更新价格出错", ex);
            }
        }

        static public bool updateProductName(long typeCID, string NewProductName)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    long autoID = db.UpdateProductName(NewProductName, typeCID);
                    if (autoID > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("修改规格出错", ex);
            }
        }

        static public bool updateClassify(long productCID,string productName, long typeCID
                                        , string typeName, double defaultPrice, string unit)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    OleDbConnection conn = db.Connection;
                    conn.Open();
                    OleDbTransaction tr = conn.BeginTransaction();
                    db.Transaction = tr;
                    long autoID = db.UpdateClassify(productName, null, null, productCID);
                    if (autoID == 0)
                    {
                        tr.Rollback();
                        return false;
                    }
                    autoID = db.UpdateClassify(typeName, defaultPrice, unit, typeCID);
                    if (autoID == 0)
                    {
                        tr.Rollback();
                        return false;
                    }
                    tr.Commit();
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("修改材料出错", ex);
            }
        }

        static public bool deleteMainClass(long mainCID)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    OleDbConnection conn = db.Connection;
                    conn.Open();
                    OleDbTransaction tr = conn.BeginTransaction();
                    db.Transaction = tr;
                    long autoID = db.DeleteMainClass(mainCID);
                    if (autoID == 0)
                    {
                        tr.Rollback();
                        return false;
                    }
                    autoID = db.ChangeParent(-1, mainCID);
                    if (autoID == 0)
                    {
                        tr.Rollback();
                        return false;
                    }
                    autoID = db.UpdateSerialsMainClass(mainCID);
                    if (autoID>=0)
                    {
                        tr.Commit();
                        return true;
                    }
                    else
                    {
                        tr.Rollback();
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("删除大类出错", ex);
            }
        }

        static public bool ChangeParentById(long[] CID, long mainCID)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    OleDbConnection conn = db.Connection;
                    conn.Open();
                    OleDbTransaction tr = conn.BeginTransaction();
                    db.Transaction = tr;
                    long autoID = 0;
                    foreach (long c in CID)
                    {
                        autoID += db.ChangeParentById(mainCID, c);
                    }
                    if (autoID == CID.Length)
                    {
                        tr.Commit();
                        return true;
                    }
                    else
                    {
                        tr.Rollback();
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("迁移材料大类出错", ex);
            }
        }

        static public int initDB(string[] line)
        {
            try
            {
                using (ClassifyTableAdapter db = new ClassifyTableAdapter())
                {
                    long mid = 0, pid = 0, tid = 0, autoID=0 ,oid=0;
                    string pname = "";
                    foreach (string item in line)
                    {
                        if (item == "") { continue; }
                        string[] value = item.Split(',');
                        OleDbConnection conn = db.Connection;
                        conn.Open();
                        OleDbTransaction tr = conn.BeginTransaction();
                        db.Transaction = tr;
                        OleDbCommand cmd = null;
                        if (value.Length>2 && value[0] != "" && value[1] == "" && value[2] == "" && value[3] == "" && value[4] == "")//添加大类
                        {
                            autoID = db.InsertClassify(value[0], 0, 1, null, null);
                            if (autoID > 0)
                            {
                                cmd = conn.CreateCommand();
                                cmd.Transaction = tr;
                                cmd.CommandText = @"select @@identity";
                                autoID = long.Parse(cmd.ExecuteScalar().ToString());
                            }
                            mid = autoID;
                            if (autoID <= 0)
                            {
                                tr.Rollback();
                                return -1;
                            }
                            tr.Commit();
                            conn.Close();
                            continue;
                        }
                        else
                        {
                            if (pname != value[0] && value[0] != "")//添加材料名
                            {
                                autoID = db.InsertClassify(value[0], mid, 2, null, null);
                                if (autoID > 0)
                                {
                                    cmd = conn.CreateCommand();
                                    cmd.Transaction = tr;
                                    cmd.CommandText = @"select @@identity";
                                    autoID = long.Parse(cmd.ExecuteScalar().ToString());
                                }
                                pid = autoID;
                                if (autoID <= 0)
                                {
                                    tr.Rollback();
                                    return -2;
                                }
                                pname = value[0];
                            }
                            //添加规格、价格、单位
                            double p = 0;
                            double.TryParse(value[3], out p);
                            if (value[1]=="")
                            {
                                value[1] = "常规";
                            }
                            autoID = db.InsertClassify(value[1], pid, 3, p, value[2]);
                            if (autoID > 0)
                            {
                                cmd = conn.CreateCommand();
                                cmd.Transaction = tr;
                                cmd.CommandText = @"select @@identity";
                                autoID = long.Parse(cmd.ExecuteScalar().ToString());
                            }
                            tid = autoID;
                            if (autoID <= 0)
                            {
                                tr.Rollback();
                                return -3;
                            }
                            tr.Commit();
                            conn.Close();
                            int a = 0;
                            int.TryParse(value[4], out a);
                            if (a > 0)
                            {
                                using (InsertInOrderTableAdapter odb = new InsertInOrderTableAdapter())
                                {
                                    OleDbConnection oconn = odb.Connection;
                                    OleDbCommand ocmd = null;
                                    oconn.Open();
                                    OleDbTransaction otr = oconn.BeginTransaction();
                                    odb.Transaction = otr;
                                    int maxSerials=(int)odb.GetMaxSerials();
                                    if (maxSerials<100)
                                    {
                                        maxSerials = DateTime.Now.Year * 10000 + 1;
                                    }
                                    maxSerials = Math.Max(DateTime.Now.Year * 10000 + 1, maxSerials);
                                    autoID = odb.InsertInOrder("初始化入库单", 0, maxSerials);
                                    if (autoID > 0)
                                    {
                                        ocmd = oconn.CreateCommand();
                                        ocmd.Transaction = otr;
                                        ocmd.CommandText = @"select @@identity";
                                        autoID = long.Parse(ocmd.ExecuteScalar().ToString());
                                    }
                                    oid = autoID;
                                    if (autoID <= 0)
                                    {
                                        otr.Rollback();
                                        return -4;
                                    }
                                    autoID = odb.InsertInSerials(mid, pid, tid, pname + " " + value[1], a, p, oid,"初始化库存");
                                    if (autoID <= 0)
                                    {
                                        otr.Rollback();
                                        return -5;
                                    }
                                    otr.Commit();
                                    oconn.Close();
                                }
                            }
                        }
                    }
//                     if (autoID == CID.Length)
//                     {
//                         tr.Commit();
//                         return true;
//                     }
//                     else
//                     {
//                         tr.Rollback();
//                         return false;
//                     }
                }
                return 1;

            }
            catch (Exception ex)
            {
                throw new Exception("初始化数据库出错", ex);
            }
        }

    }
}
